package com.pucheng.bracelet.ui.activity.clock

import android.os.Bundle
import android.os.ProxyFileDescriptorCallback
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.ext.CLOCK_DATA_INDEX_KEY
import com.pucheng.bracelet.app.ext.CLOCK_DATA_KEY
import com.pucheng.bracelet.app.util.click
import com.pucheng.bracelet.databinding.FragmentClockEditBinding
import com.pucheng.bracelet.ui.adapter.ClockData
import com.pucheng.bracelet.ui.dialog.OnWeekRepeatCallback
import com.pucheng.bracelet.ui.dialog.WeekDaySelectDialog
import com.pucheng.bracelet.viewmodel.clock.ClockViewModel
import me.hgj.jetpackmvvm.util.ActivityMessenger.finish

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [ClockEditFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class ClockEditFragment(private val callback: () -> Unit) : Fragment() {
    private lateinit var mViewModel: ClockViewModel
    private lateinit var mViewBinding: FragmentClockEditBinding
    private var editClockDataIndex: Int? = null
    private var editClockData: ClockData? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            editClockDataIndex = it.getInt(CLOCK_DATA_INDEX_KEY)
            editClockData = it.getSerializable(CLOCK_DATA_KEY) as ClockData

        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mViewModel = ViewModelProvider(requireActivity()).get(ClockViewModel::class.java)
        mViewBinding = FragmentClockEditBinding.inflate(
            LayoutInflater.from(requireContext()),
            container,
            false
        )
        mViewBinding.apply {
            mViewModel.initTime(editClockDataIndex, editClockData)
            activity?.let { mViewModel.getWeekRepeatData(editClockData!!.weekRepeat) }
            viewModel = mViewModel
            lifecycleOwner = viewLifecycleOwner
            tvCancel.click {
                callback?.invoke()
            }
            tvSave.click {
                mViewModel.saveClock()
                callback?.invoke()
            }
            tvClockRepeat.click {
                activity?.let { it1 ->
                    val weekDaySelectDialog = WeekDaySelectDialog(it1,editClockData!!.weekRepeat)
                    weekDaySelectDialog.show()
                    weekDaySelectDialog.setOnWeekRepeatCallback(object : OnWeekRepeatCallback {
                        override fun onWeekRepeat(weekRepeat: Int) {
                            mViewModel.getWeekRepeatData(weekRepeat)
                        }
                    })
                }
            }
            wvAmpm.setCyclic(false)
            wvHour.setCyclic(false)
            wvMinute.setCyclic(false)
            wvAmpm.setOnItemSelectedListener { index ->
                mViewModel.mApIndex.value = index
            }
            wvHour.setOnItemSelectedListener { index ->
                mViewModel.mHourIndex.value = index
            }
            wvMinute.setOnItemSelectedListener { index ->
                mViewModel.mMinuteIndex.value = index
            }

            mViewModel.apply {
                timeAp.observe(viewLifecycleOwner) {
                    wvAmpm.adapter = ArrayWheelAdapter(it)
                }
                timeHour.observe(viewLifecycleOwner) {
                    wvHour.adapter = ArrayWheelAdapter(it)
                }
                timeMinute.observe(viewLifecycleOwner) {
                    wvMinute.adapter = ArrayWheelAdapter(it)
                }
                timeClock.observe(viewLifecycleOwner) {
                    wvAmpm.currentItem = it[0]
                    wvHour.currentItem = it[1]
                    wvMinute.currentItem = it[2]
                }
                weekRepeatStr.observe(viewLifecycleOwner) {
                    tvClockRepeatRight.text = it
                }
                mApIndex.observe(viewLifecycleOwner) {
                    wvAmpm.currentItem = it
                }
                mHourIndex.observe(viewLifecycleOwner) {
                    wvHour.currentItem = it
                }
                mMinuteIndex.observe(viewLifecycleOwner) {
                    wvMinute.currentItem = it
                }
            }
        }
        return mViewBinding.root
    }


    companion object {
        @JvmStatic
        fun newInstance(
            position: Int,
            clockData: ClockData,
            callback: () -> Unit
        ): ClockEditFragment {
            return ClockEditFragment(callback).apply {
                arguments = Bundle().apply {
                    putInt(CLOCK_DATA_INDEX_KEY, position)
                    putSerializable(CLOCK_DATA_KEY, clockData)
                }
            }
        }
    }
}